/**
 * 
 */
package org.wicket.commons.config;

import java.net.MalformedURLException;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author Emmanuel Nollase - emanux
 *
 */
public final class WfConfigInitializer implements ServletContextListener {

private static final Log logger = LogFactory.getLog(WfConfigInitializer.class);
    
    @SuppressWarnings(value = {"unchecked"})
    private static void setUpConfiguration(final ServletContext servletContext) {
        final Set<String> configFiles = (Set<String>)
                servletContext.getResourcePaths("/jbpm-mapping/");
        if (null != configFiles) {
            try {
                for (String file : configFiles) {
                    if (file.endsWith(".xml")) {
                        logger.info(new StringBuilder(64).append("Loading config file: ")
                                .append(file).toString());
                        WfConfiguration.addConfigFile(servletContext.getResource(file));
                    }
                }
            }
            catch (MalformedURLException e) {
                logger.error("MalformedURLException => " + e.getMessage(), e);
            }
        }
    }

     
	/* (non-Javadoc)
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
	 */
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
	 */
	public void contextInitialized(ServletContextEvent sce) {
		setUpConfiguration(sce.getServletContext());
		logger.debug("Application is starting...");
	}

}
